package com.ruoyi.csdn.controller;


import com.ruoyi.common.utils.aes.AESEncryptor;
import com.ruoyi.csdn.mapper.CsdnArticleMapper;
import com.ruoyi.csdn.mapper.CsdnLogMapper;
import com.ruoyi.csdn.mapper.CsdnUserMapper;
import com.ruoyi.csdn.socket.WebSocketServer;
import com.ruoyi.common.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author Admin小闫
 * @Date 2024/12/30
 */
@Controller
@RequestMapping("/csdnScoket/websocket")
public class CsdnWebSocketController extends BaseController
{

    //文章表
    @Autowired
    private CsdnArticleMapper csdnArticleMapper;
    //用户表
    @Autowired
    private CsdnUserMapper csdnUserMapper;
    //日志表
    @Autowired
    private CsdnLogMapper csdnLogMapper;
    // 密码加/解密
    @Autowired
    private AESEncryptor aesEncryptor;


    //推送数据接口
    /**
     * message :格式()
     * @param cid
     * @param message
     * @return
     */
    @ResponseBody
    @RequestMapping("/push/{cid}")
    public Map<String, Object> pushToWeb(@PathVariable String cid, String message) {
        System.out.println("controller--------------------");

        if (message == null) {
            message = "服务端返回!!!";
        }
        Map<String, Object> result = new HashMap<>();
        try {
            WebSocketServer.sendInfo(message, cid);
            result.put("code", 200);
            result.put("msg", "success");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发送测试数据
     */
    @RequestMapping("/send/{cid}")
    public void send(@PathVariable String cid) {
        try {
            String message = "{\n" +
                    "        \"type\": 1,\n" +
                    "        \"message\": {\n" +
                    "            \"id\": 9,\n" +
                    "            \"title\": \"Markdown \\u8bed\\u6cd5\\u7b80\\u4ecb\",\n" +
                    "            \"content\": \"# Markdown \\u8bed\\u6cd5\\u7b80\\u4ecb\\n\\n## \\u7b80\\u4ecb\\n\\nMarkdown \\u662f\\u4e00\\u79cd\\u8f7b\\u91cf\\u7ea7\\u7684\\u6807\\u8bb0\\u8bed\\u8a00\\uff0c\\u5b83\\u5141\\u8bb8\\u4eba\\u4eec\\u4f7f\\u7528\\u6613\\u8bfb\\u6613\\u5199\\u7684\\u7eaf\\u6587\\u672c\\u683c\\u5f0f\\u7f16\\u5199\\u6587\\u6863\\uff0c\\u7136\\u540e\\u8f6c\\u6362\\u6210\\u7ed3\\u6784\\u5316\\u7684HTML\\uff08\\u8d85\\u6587\\u672c\\u6807\\u8bb0\\u8bed\\u8a00\\uff09\\u3002\\u8fd9\\u79cd\\u8bed\\u6cd5\\u7531\\u7ea6\\u7ff0\\u00b7\\u683c\\u9c81\\u4f2f\\uff08John Gruber\\uff09\\u521b\\u5efa\\uff0c\\u65e8\\u5728\\u8ba9\\u7eaf\\u6587\\u672c\\u5185\\u5bb9\\u5728\\u5404\\u79cd\\u8bbe\\u5907\\u4e0a\\u90fd\\u80fd\\u4fdd\\u6301\\u826f\\u597d\\u7684\\u53ef\\u8bfb\\u6027\\uff0c\\u5e76\\u4e14\\u6613\\u4e8e\\u8f6c\\u6362\\u4e3a\\u5176\\u4ed6\\u683c\\u5f0f\\uff0c\\u5982HTML\\u3001PDF\\u7b49\\u3002\\n\\n## \\u6807\\u9898\\n\\n\\u8981\\u521b\\u5efa\\u6807\\u9898\\uff0c\\u53ef\\u4ee5\\u5728\\u6587\\u672c\\u524d\\u6dfb\\u52a0 `#` \\u7b26\\u53f7\\u3002`#` \\u7684\\u6570\\u91cf\\u51b3\\u5b9a\\u4e86\\u6807\\u9898\\u7684\\u7ea7\\u522b\\uff0c\\u4ece1\\u52306\\uff0c\\u5176\\u4e2d9\\u662f\\u6700\\u9ad8\\u7ea7\\u522b\\u7684\\u6807\\u9898\\u3002\\n\\n```markdown\\n# \\u8fd9\\u662f\\u4e00\\u7ea7\\u6807\\u9898\\n## \\u8fd9\\u662f\\u4e8c\\u7ea7\\u6807\\u9898\\n### \\u8fd9\\u662f\\u4e09\\u7ea7\\u6807\\u9898\\n#### \\u8fd9\\u662f\\u56db\\u7ea7\\u6807\\u9898\\n##### \\u8fd9\\u662f\\u4e94\\u7ea7\\u6807\\u9898\\n###### \\u8fd9\\u662f\\u516d\\u7ea7\\u6807\\u9898\",\n" +
                    "            \"tags\": \"mark\\uff0c\\u8bed\\u6cd5\\uff0c\\u4ee3\\u7801\",\n" +
                    "            \"summary\": \"Markdown \\u662f\\u4e00\\u79cd\\u8f7b\\u91cf\\u7ea7\\u7684\\u6807\\u8bb0\\u8bed\\u8a00\\uff0c\\u65e8\\u5728\\u63d0\\u4f9b\\u4e00\\u79cd\\u6613\\u8bfb\\u6613\\u5199\\u7684\\u7eaf\\u6587\\u672c\\u683c\\u5f0f\\u6765\\u7f16\\u5199\\u6587\\u6863\\uff0c\\u5e76\\u80fd\\u591f\\u65b9\\u4fbf\\u5730\\u8f6c\\u6362\\u6210\\u7ed3\\u6784\\u5316\\u7684HTML\\u3002\",\n" +
                    "            \"type\": 1,\n" +
                    "            \"visibility\": 1,\n" +
                    "            \"publish_time\": 202412271200,\n" +
                    "            \"userid\": 2,\n" +
                    "            \"is_publish\": 0\n" +
                    "        }\n" +
                    "    }";
            WebSocketServer.sendInfo(message, cid);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
